﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace TopShooter
{
	public class ExplosiveBarrel : Entity
	{
		public ExplosiveBarrel()
			: base()
		{
		}

		public override void OnDestroyed()
		{
			var effect = new Animations.Shake(this);
			effect.Removed = _OnshakeDone;
			Effects.Add(effect);
		}

		public override void Draw(SpriteBatch spriteBatch, Vector2 offset)
		{
			base.Draw(spriteBatch, offset);
		}

		private void _OnshakeDone(Animations.EntityAnimation effect)
		{
			if (Scene == null)
				return;
			float collisionDistance2 = 20 * Tile.Size.X;
			collisionDistance2 *= collisionDistance2;

			for (int i = Scene.Entities.Count - 1; i >= 0; i--)
			{
				Entity e = Scene.Entities[i];
				if (!e.IsDestructible)
					continue;
				Vector2 offset = e.Location - Location;
				float dist2 = offset.LengthSquared();
				if (dist2 <= collisionDistance2)
				{
					float basedamage = 10;
					int damage = (int)(basedamage * (collisionDistance2 / dist2));
					//e.Health -= (int)damage;
					e.TakeDamage(damage, this);
				}
			}

			base.OnDestroyed();

		}

	}
}
